﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Amba.Workflow.Models;
using Orchard;
using Orchard.Data;

namespace Amba.Workflow.Services
{
    public interface IEventLogService : IDependency
    {
        IEnumerable<WorkflowLogRecord> GetRecords();
        void CreateEventLogRecord(WorkflowLogRecord record);

        void Delete(int id);
    }

    public class EventLogService : IEventLogService
    {
        private readonly IRepository<WorkflowLogRecord> _eventLogRepo;

        public EventLogService(IRepository<WorkflowLogRecord> eventLogRepo)
        {
            _eventLogRepo = eventLogRepo;
        }

        public IEnumerable<WorkflowLogRecord> GetRecords()
        {
            return _eventLogRepo.Table.OrderByDescending(x => x.LogUtcDate).Take(1000);
        }

        public void CreateEventLogRecord(WorkflowLogRecord record)
        {
            _eventLogRepo.Create(record);
        }

        public void Delete(int id)
        {
            var record = _eventLogRepo.Get(id);
            if (record != null)
                _eventLogRepo.Delete(record);
        }

        
    }
}